﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _00_计算器_反射
{
    public partial class Form1 : Form
    {
        //用来记录下指定文件夹下所有类库的信息
        List<ClassLibraryInfo> classLibraryInfos = new List<ClassLibraryInfo>();

        public Form1()
        {
            InitializeComponent();
            //遍历指定路径下的所有dll文件，然后将其类库信息添加到集合对象中
            LoadPlugins(@"./插件");

        }

        private void LoadPlugins(string path)
        {
            //获取指定路径下的所有dll文件
            string[] files = System.IO.Directory.GetFiles(path, "*.dll");

            //遍历每一个动态链接库
            foreach (string file in files)
            {
                //使用代码加载加载动态链接库
                Assembly assembly = Assembly.LoadFrom(file);

                //获取动态链接库中的所有类
                Type[] types = assembly.GetTypes();

                //遍历每一个类
                foreach (Type type in types)
                {
                    //判断类是否实现了IOperation接口
                    if (type.GetInterface("IOperation.Ioperation") != null)
                    {
                        //创建当前类的实例
                        IOperation.Ioperation obj = (IOperation.Ioperation)Activator.CreateInstance(type);

                        //创建一个ClassLibraryInfo对象
                        ClassLibraryInfo info = new ClassLibraryInfo(file,type.FullName,obj.Name);
                        //添加到集合中
                        classLibraryInfos.Add(info);


                        //将类的名称添加到下拉列表中
                        comboBox1.Items.Add(obj.Name);
                    }
                }


            }

        }
        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //获取下拉框选的文本
            string selectOperator = this.comboBox1.SelectedItem?.ToString();

            //根据下拉框选择的文本，获取对应的类库信息
            ClassLibraryInfo info = classLibraryInfos.FirstOrDefault(c => c.Name == selectOperator);

            //判断是否获取到类库信息
            if (info != null)
            {
                //使用代码加载动态链接库(反射)
                Assembly assembly = Assembly.LoadFrom(info.DllPath);
                //获取动态链接库中的指定类（命名空间.类名）
                Type type = assembly.GetType(info.ClassName);
                //创建当前类的实例
                IOperation.Ioperation obj = (IOperation.Ioperation)Activator.CreateInstance(type);

                //对属性进行赋值
                obj.NumberLeft = int.Parse(this.textBox1.Text);
                obj.NumberRight = int.Parse(this.textBox2.Text);

                //获取计算结果
                this.textBox3.Text = obj.GetResult().ToString();

            }
            else
            { 
                MessageBox.Show("找不到，请检查是否有指定的插件");
            }

        }
    }
}
